<template>
  <!-- 
    action 上传的地址
    show-file-list 是否显示已上传的文件列表
    on-success 文件上传成功时的钩子
    before-upload 文件上传之前的钩子,参数file,返回false停止上传，否则继续
    http-request 覆盖默认的上传行为，可以自定义上传的实现
    file-list 上传文件对象列表
 -->
  <div class="imageUpload-container">
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="upLoad"
      @mouseenter.native="boxShow = imageUrl ? true : false"
      @mouseleave.native="boxShow = false"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <!-- 有什么好的方法，可以不点击到下面 -->
      <div class="box" v-if="boxShow" @click.stop="111">
        <i class="el-icon-zoom-in" @click.stop="preview"></i>
        <i class="el-icon-delete" @click.stop="del"></i>
      </div>
    </el-upload>
    <el-dialog title="图片预览" :visible.sync="dialogVisible">
      <img :src="imageUrl" alt="" style="display:block;width:100%" />
    </el-dialog>
  </div>
</template>

<script>
// 导入腾讯云--对象储存--js sdk
import COS from 'cos-js-sdk-v5'
// 创建实例化对象
const cos = new COS({
  SecretId: 'AKID3uUnqc7MJ0QZR2MPBguvmQuCH6gz0kFT',
  SecretKey: 'yg8JDqCI7sMZVChz1mGTP4SZQ0JL6bpm'
})
export default {
  data () {
    return {
      imageUrl: '',
      boxShow: false,
      dialogVisible: false
    }
  },
  methods: {
    // 点击预览
    preview () {
      console.log(111)
      this.dialogVisible = true
    },
    // 点击删除
    del () {
      console.log(222)
      this.boxShow = false
      this.imageUrl = ''
    },

    beforeAvatarUpload (file) {
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/webp'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义上传
    upLoad (params) {
      // 上传对象
      cos.putObject(
        {
          Bucket: 'luoyu-1306481846' /* 必须 存储桶名称 */,
          Region: 'ap-shenzhen-fsi' /* 存储桶所在地域，必须字段 */,
          Key: params.file.name /* 必须 上传文件对象的名称*/,
          StorageClass: 'STANDARD',
          Body: params.file // 上传文件对象
          // 上传进度条的回调
          //   onProgress: function (progressData) {
          //     console.log(JSON.stringify(progressData))
          //   }
        },
        // 上传成功/失败的回调
        (err, data) => {
          console.log(err || data)
          if (!err) {
            this.imageUrl = 'http://' + data.Location
          }
        }
      )
    }
  }
}
</script>

<style lang="scss">
// 1.点击上传，图片凸显，加号消失
// 2.鼠标移入，阴影盒子出现
// 3.鼠标移入，图片存在，阴影盒子出现，否则消失
.imageUpload-container {
  .box {
    width: 178px;
    height: 178px;
    background-color: rgba(0, 0, 0, 0.4);
    position: absolute;
    left: 0;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 40px;
    i {
      padding: 10px;
      color: skyblue;
    }
  }
  .avatar-uploader {
    width: 178px;
    height: 178px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
